package cn.wolfcode.service.impl;

import cn.wolfcode.entity.Destination;
import cn.wolfcode.entity.Travel;
import cn.wolfcode.entity.Userinfo;
import cn.wolfcode.mapper.TravelMapper;
import cn.wolfcode.query.TravelCondition;
import cn.wolfcode.query.TravelQuery;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.ITravelContentService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserinfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@Service
public class TravelServiceImpl extends ServiceImpl<TravelMapper, Travel> implements ITravelService {

    @Autowired
    private IUserinfoService userinfoService;

    @Override
    public Page<Travel> queryPage(TravelQuery qo) {

        Page<Travel> page = new Page<>();
        page.setCurrent(qo.getCurrentPage());
        page.setSize(qo.getPageSize());

        QueryWrapper<Travel> wrapper = new QueryWrapper<>();
        wrapper.eq(qo.getDestId() != null && !qo.getDestId().equals(""), "dest_idf", qo.getDestId());

        TravelCondition dayConditon = TravelCondition.DAY_MAP.get(qo.getDayType());
        if (dayConditon != null) {
            wrapper.ge("day", dayConditon.getMin());
            wrapper.le("day", dayConditon.getMax());
        }

        TravelCondition avgCondition = TravelCondition.AVG_MAP.get(qo.getConsumeType());
        if (avgCondition != null) {
            wrapper.ge("avg_consume", avgCondition.getMin());
            wrapper.le("avg_consume", avgCondition.getMax());
        }

        TravelCondition timeCondition = TravelCondition.TIME_MAP.get(qo.getTravelTimeType());
        if (timeCondition != null) {
            wrapper.ge("MONTH(travel_time)", timeCondition.getMin());
            wrapper.le("MONTH(travel_time)", timeCondition.getMax());
        }

        wrapper.orderByDesc(qo.getOrderBy());


        Page<Travel> travelPage = page(page, wrapper);
        for (Travel record : travelPage.getRecords()) {
            Long authorId = record.getAuthorId();
            record.setAuthor(userinfoService.getById(authorId));
        }
        return travelPage;

    }

    @Override
    public List<Travel> queryViewNumTop3(Long destId) {
        QueryWrapper<Travel> wrapper = new QueryWrapper<>();
        wrapper.eq("dest_id", destId);
        wrapper.orderByDesc("viewnum");
        wrapper.last("limit 3");
        return list(wrapper);
    }

    @Autowired
    private ITravelContentService travelContentService;
    @Autowired
    private IDestinationService destinationService;
    @Override
    public Long saveOrUpdate(Travel travel, Userinfo userinfo) {
        Destination dest = destinationService.getById(travel.getDestId());
        // 为travel赋值
        travel.setDestName(dest.getName());
        travel.setCreateTime(new Date());
        travel.setLastUpdateTime(new Date());
        travel.setViewnum(0);
        travel.setReplynum(0);
        travel.setSharenum(0);
        travel.setFavornum(0);
        travel.setThumbsupnum(0);
        travel.setAuthor(userinfoService.getById(userinfo.getId()));
        travel.setAuthorId(userinfo.getId());
        saveOrUpdate(travel);
        travel.getContent().setId(travel.getId());
        travelContentService.saveOrUpdate(travel.getContent());
        return travel.getId();

    }

    @Override
    public List<Travel> queryByDestId(Long id) {

        QueryWrapper<Travel> wrapper = new QueryWrapper<>();
        wrapper.eq("dest_id", id);
        return list(wrapper);
    }
}
